package com.example.backblog.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;

/**
 * @description: 多数据源数据库配置
 * @Date: 2020/11/11 14:25
 */
@Configuration
public class DataSourceConfig {

    /**
     * 体系模块的数据源(主数据源)
     * 使用@Primary标记主数据源；主数据源只能有一个
     * @return
     */
    @Primary
    @Bean(name = "mainDataSource")
    @Qualifier("mainDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.main")
    public DataSource userDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 用户模块数据源
     * @return
     */
    @Bean(name = "secondDataSource")
    @Qualifier("secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource systemDataSource() {
        return DataSourceBuilder.create().build();
    }

}

